package com.chutoutian.controller;

import com.chutoutian.common.Code;
import com.chutoutian.exception.VailException;
import com.chutoutian.po.Orders;
import com.chutoutian.service.CartService;
import com.chutoutian.vo.CartItem;
import com.chutoutian.vo.ResponseMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping(value = "/v1")
public class CartController {

    private Logger logger = LoggerFactory.getLogger(CartController.class.getName());

    @Resource
    private CartService cartService;

    @PostMapping(value = "/cart/item")
    public Object addItem(@RequestParam String id, HttpSession session) {
        cartService.addItem(id, session);
        return new ResponseMessage.Builder().code(Code.CODE_200).message("已添加").bulid();
    }

    @DeleteMapping(value = "/cart/item")
    public Object deleteItem(@RequestParam String id, HttpSession session) {
        cartService.deleteItem(id, session);
        return new ResponseMessage.Builder().code(Code.CODE_200).bulid();
    }

    @PutMapping(value = "/cart/item")
    public Object updateItem(@RequestParam String id, @RequestParam Integer num, HttpSession session) {
        cartService.updateItem(id, num, session);
        return new ResponseMessage.Builder().code(Code.CODE_200).bulid();
    }

    @GetMapping(value = "/cart/orders")
    public Object getOrders(HttpSession session) {
        try {
            return new ResponseMessage.Builder().code(Code.CODE_200).data(cartService.getOrders(session)).bulid();
        } catch (VailException e) {
            return new ResponseMessage.Builder().code(Code.CODE_400).message(e.getMessage()).bulid();
        } catch (Exception e) {
            logger.error("server error", e);
            return new ResponseMessage.Builder().code(Code.CODE_500).bulid();
        }
    }



    @PostMapping(value = "/cart/orders")
    public Object addOrders(HttpSession session) {
        try {
            Orders orders = cartService.addOrders(session);
            return new ResponseMessage.Builder().code(Code.CODE_200).data(orders).bulid();
        } catch (Exception e) {
            logger.error("server error", e);
            return new ResponseMessage.Builder().code(Code.CODE_500).bulid();
        }
    }

    @GetMapping(value = "/cart")
    public Object get(HttpSession session) {
        List<CartItem> cartItemList = cartService.get(session);
        return new ResponseMessage.Builder().code(Code.CODE_200).data(cartItemList).bulid();
    }

    @GetMapping(value = "/cart/num")
    public Object getNum(HttpSession session) {
        Integer sum = cartService.getNum(session);
        return new ResponseMessage.Builder().code(Code.CODE_200).data(sum).bulid();
    }

    @PutMapping(value = "/cart/item/selected")
    public Object selectedItem(@RequestParam String ids, HttpSession session) {
        cartService.selectedItem(ids, session);
        return new ResponseMessage.Builder().code(Code.CODE_200).bulid();
    }

    @PutMapping(value = "/cart/item/unSelected")
    public Object unSelectedItem(@RequestParam String ids, HttpSession session) {
        cartService.unSelectedItem(ids, session);
        return new ResponseMessage.Builder().code(Code.CODE_200).bulid();
    }


}
